//
//  EverydayExpressViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/8/27.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "EverydayExpressViewController.h"
#import "SearchExpressViewController.h"
#import "ExpressDetailViewController.h"
#import "EverydayView.h"
#import "Singleton.h"
#import "EveryGroup.h"

@interface EverydayExpressViewController ()<EverydayViewDelegate,ExpressDetailViewControllerDelegate> {
    EverydayView *_signinEverydayView;
    EverydayView *_noTakeEverydayView;
    EverydayView *_allEverydayView;
    
    EverydayView *_currentView;
}

@end

@implementation EverydayExpressViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"快件管理";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _multipleTabView.tabArray = @[@"已签收",@"未签收", @"全部"];
    float height = [UIScreen mainScreen].bounds.size.height - 44 - 64;
    _bgScrollView.contentSize = CGSizeMake(320 * 3, height);

    _signinEverydayView = [[EverydayView alloc] initWithFrame:CGRectMake(0, 0, 320, height)];
    _signinEverydayView.expressType = @"02";
    _signinEverydayView.everydayViewDelegate = self;
    [_bgScrollView addSubview:_signinEverydayView];

    _noTakeEverydayView = [[EverydayView alloc] initWithFrame:CGRectMake(320, 0, 320, height)];
    _noTakeEverydayView.expressType = @"01";
    _noTakeEverydayView.everydayViewDelegate = self;
    [_bgScrollView addSubview:_noTakeEverydayView];
    
    _allEverydayView = [[EverydayView alloc] initWithFrame:CGRectMake(640, 0, 320, height)];
    _allEverydayView.expressType = @"00";
    _allEverydayView.everydayViewDelegate = self;
    [_bgScrollView addSubview:_allEverydayView];
    
    [self setRightButton];
    [self handelNetWorkMethod];
}

- (void)setRightButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 22, 22);
    [button setBackgroundColor:[UIColor clearColor]];
    [button setImage:[UIImage imageNamed:@"nav_icon_search"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"nav_icon_search_pre"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(pullSearchVC) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)pullSearchVC {
    [MobClick event:@"kuaijianguanli_img_chaxun"];
    SearchExpressViewController *searchEVC = [[SearchExpressViewController alloc] initWithNibName:@"SearchExpressViewController" bundle:nil];
    [self.navigationController pushViewController:searchEVC animated:YES];
}

- (void)pullExpressDetailVC:(NSArray *)groupList IndexPath:(NSIndexPath *)indexPath {
    EveryGroup *group = [groupList objectAtIndex:indexPath.section];
    EveryExpress *express = [group.expressList objectAtIndex:indexPath.row];
    ExpressDetailViewController *expressDVC = [[ExpressDetailViewController alloc] initWithNibName:@"ExpressDetailViewController" bundle:nil];
    expressDVC.inwayExpressId = express.inwayExpressId;
    expressDVC.indexPath = indexPath;
    expressDVC.delegate = self;
    [self.navigationController pushViewController:expressDVC animated:YES];
}

#pragma mark MultipleTabViewDelegate
- (void)buttonClickIndex:(NSInteger)currentIndex {
    [_bgScrollView setContentOffset:CGPointMake(320 * currentIndex, 0) animated:YES];
    if (currentIndex == 0) {
        _bgScrollView.bounces = NO;
    } else {
        _bgScrollView.bounces = YES;
    }
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = (NSInteger)scrollView.contentOffset.x/320;
    _multipleTabView.index = index;
    if (index == 0) {
        _bgScrollView.bounces = NO;
    } else {
        _bgScrollView.bounces = YES;
    }
}

#pragma mark EverydayViewDelegate
- (void)everydayView:(EverydayView *)view IndexPath:(NSIndexPath *)indexPath {
    _currentView = view;
    [self pullExpressDetailVC:view.everyGroupList IndexPath:indexPath];
}

#pragma mark ExpressDetailViewControllerDelegate
- (void)smsSendSuccess:(NSIndexPath *)indexPath {
    EveryGroup *group = [_currentView.everyGroupList objectAtIndex:indexPath.section];
    EveryExpress *express = [group.expressList objectAtIndex:indexPath.row];
    express.smsSendType = 0;
    [express saveOrUpdate];
    [_currentView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    EverydayView *anotherView = nil;
    if (_currentView == _noTakeEverydayView) {
        anotherView = _allEverydayView;
    } else {
        anotherView = _noTakeEverydayView;
    }
    for (int i = 0; i < anotherView.everyGroupList.count; i ++) {
        EveryGroup *anotherGroup = [anotherView.everyGroupList objectAtIndex:i];
        for (int j = 0; j < anotherGroup.expressList.count; j ++) {
            EveryExpress *anotherExpress = [anotherGroup.expressList objectAtIndex:j];
            if ([anotherExpress.inwayExpressId isEqualToString:express.inwayExpressId]) {
                anotherExpress.smsSendType = 0;
                [anotherExpress saveOrUpdate];
                [anotherView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:j inSection:i]] withRowAnimation:UITableViewRowAnimationAutomatic];
                break;
            }
        }
    }
}

- (void)takeExpressSuccess:(NSIndexPath *)indexPath {
    EveryGroup *group = [_currentView.everyGroupList objectAtIndex:indexPath.section];
    EveryExpress *express = [group.expressList objectAtIndex:indexPath.row];
    express.expressType = @"0033";
    if (_currentView == _allEverydayView) {
        [express saveOrUpdate];
        [_allEverydayView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self reloadNotakeEveryDayView:express.inwayExpressId];
    } else {
        [group.expressList removeObject:express];
        [express deleteObject];
        group.dayCount --;
        if (group.dayCount == 0) {
            [_noTakeEverydayView.everyGroupList removeObject:group];
            [_noTakeEverydayView reloadData];
        } else {
            [_noTakeEverydayView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        for (int i = 0; i < _allEverydayView.everyGroupList.count; i ++) {
            EveryGroup *anotherGroup = [_allEverydayView.everyGroupList objectAtIndex:i];
            for (int j = 0; j < anotherGroup.expressList.count; j ++) {
                EveryExpress *anotherExpress = [anotherGroup.expressList objectAtIndex:j];
                if ([anotherExpress.inwayExpressId isEqualToString:express.inwayExpressId]) {
                    anotherExpress.expressType = @"0033";
                    [anotherExpress saveOrUpdate];
                    [_allEverydayView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:j inSection:i]] withRowAnimation:UITableViewRowAnimationAutomatic];
                    break;
                }
            }
        }
    }
    [_signinEverydayView reloadExpressListData];
}

- (void)reloadNotakeEveryDayView:(NSString *)inwayExpressId {
    NSIndexPath *anotherIndexPath = nil;
    for (int i = 0; i < _noTakeEverydayView.everyGroupList.count; i ++) {
        EveryGroup *anotherGroup = [_noTakeEverydayView.everyGroupList objectAtIndex:i];
        for (int j = 0; j < anotherGroup.expressList.count; j ++) {
            EveryExpress *anotherExpress = [anotherGroup.expressList objectAtIndex:j];
            if ([anotherExpress.inwayExpressId isEqualToString:inwayExpressId]) {
                anotherIndexPath = [NSIndexPath indexPathForRow:j inSection:i];
                break;
            }
        }
    }
    if (anotherIndexPath) {
        EveryGroup *anotherGroup = [_noTakeEverydayView.everyGroupList objectAtIndex:anotherIndexPath.section];
        EveryExpress *anotherexpress = [anotherGroup.expressList objectAtIndex:anotherIndexPath.row];
        [anotherGroup.expressList removeObject:anotherexpress];
        [anotherexpress deleteObject];
        anotherGroup.dayCount --;
        if (anotherGroup.dayCount == 0) {
            [_noTakeEverydayView.everyGroupList removeObject:anotherGroup];
            [_noTakeEverydayView reloadData];
        } else {
            [_noTakeEverydayView reloadSections:[NSIndexSet indexSetWithIndex:anotherIndexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
}

- (void)updateExpress:(NSIndexPath *)indexPath Telephone:(NSString *)telephone Address:(NSString *)address Position:(NSString *)position {
    EveryGroup *group = [_currentView.everyGroupList objectAtIndex:indexPath.section];
    EveryExpress *express = [group.expressList objectAtIndex:indexPath.row];
    express.expressType = @"0006";
    express.smsSendType = 0;
    [express saveOrUpdate];
    [_currentView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

- (void)dealloc {
    
}

@end
